<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\V1UserWithdrawal;
use support\exception\BusinessException;

/**
 * 会员提现服务类
 **/
class UserWithdrawalService
{

    /**
     * 申请提现
     *
     * @param array $params
     * @param array $admin
     * @return bool
     */
    public static function create($params, $admin)
    {
        list($amount, $account_id, $desc) = array_values($params);

        if ($amount > $admin['commission']) {
            throw new BusinessException('可提现佣金不足！');
        }

        $db= Util::db();

        $info = $db->table('wa_v1_user_withdrawal_account')
            ->where('id', $account_id)
            ->where('user_id', $admin['id'])
            ->first();
        if (!$info) {
            throw new BusinessException('提现账号不存在或已删除！');
        }

        $data = $params;
        $handingfee = 0;
        $handingfee = bcmul($amount, $handingfee, 2);
        $payamount = bcsub($amount, $handingfee, 2);
        $order_no = get_order_sn($admin['id']);
        $data['user_id'] = $admin['id'];
        $data['user_name'] = $admin['username'];
        $data['order_no'] = $order_no;
        $data['payamount'] = $payamount;
        $data['handingfee'] = $handingfee;
        $data['status'] = CommonConstant::DEFAULT_1;
        $data['account'] = $info->account;
        $data['account_name'] = $info->name;
        $data['desc'] = $desc;
        unset($data['account_id']);
        $logData = [
            'admin_id' => $admin['id'],
            'admin_name' => $admin['username'],
            'user_id' => $admin['id'],
            'user_name' => $admin['username'],
            'type' => CommonConstant::MONEY_LOG_TYPE_7,
            'change_type' => CommonConstant::DEFAULT_20,
            'money_before' => $admin['commission'],
            'money' => $amount,
            'memo' => '提现申请中',
        ];
        $db->transaction(function () use ($db,$data, $logData) {
            $logId = UserService::commission($logData, $logData['user_id'], []);
            $data['log_id'] = $logId;
            $db->table('wa_v1_user_withdrawal')->insert($data);
        });
        return true;
    }

    /**
     * 后台提现审核
     *
     * @param array $params
     * @param array $admin
     * @return bool
     */
    public static function audit($params, $admin)
    {
        list($id, $status, $audit_desc) = array_values($params);

        $db = Util::db();

        $info = V1UserWithdrawal::where('id', $id)->first();
        if (!$info) {
            throw new BusinessException('提现信息不存在或已删除！');
        }
        if ($info->status != CommonConstant::DEFAULT_1) {
            throw new BusinessException('提现信息状态不是待审核！');
        }

        $data = $params;
        $data['audit_time'] = date('Y-m-d H:i:s');
        unset($data['id']);
        if ($status == CommonConstant::DEFAULT_3) {
            $user = $db->table('wa_admins')->where('id', $info->user_id)->select(['id', 'username', 'commission'])->first();
            if (!$user) {
                throw new BusinessException('会员信息不存在或已删除！');
            }

            $logData = [
                'admin_id' => $admin['id'],
                'admin_name' => $admin['username'],
                'user_id' => $user->id,
                'user_name' => $user->username,
                'type' => CommonConstant::MONEY_LOG_TYPE_9,
                'change_type' => CommonConstant::DEFAULT_10,
                'money_before' => $user->commission,
                'money' => $info->amount,
                'memo' => '提现驳回',
            ];
            $db->transaction(function () use ($info, $data, $logData) {
                UserService::commission($logData, $logData['user_id'], [],['id' => $info->log_id]);
                doUpdate($data, $info);
            });
        } else {
            $logData = [
                'type' => CommonConstant::MONEY_LOG_TYPE_8,
                'memo' => '提现成功',
            ];
            $db->transaction(function () use ($db,$info, $data, $logData) {
                $db->table('wa_v1_user_commission_log')->where('id', $info->log_id)->update($logData);
                doUpdate($data, $info);
            });
        }
        return true;
    }

}
